﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Controls;

namespace NthDownload.Common.Interactivity.Behaviors
{
    public class PasswordBoxBinder : Behavior<PasswordBox>
    {
        #region Override Methods

        protected override void OnAttached()
        {
            base.OnAttached();

            AssociatedObject.Loaded -= AssociatedObjectOnLoaded;
            AssociatedObject.Loaded += AssociatedObjectOnLoaded;
        }

        #endregion Override Methods

        #region Dependency Properties

        public string Password
        {
            get { return (string)GetValue(PasswordProperty); }
            set { SetValue(PasswordProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Password.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(PasswordBoxBinder), new PropertyMetadata(""));

        #endregion Dependency Properties

        #region Event Handlers

        private void AssociatedObjectOnPasswordChanged(object sender, RoutedEventArgs routedEventArgs)
        {
            Password = AssociatedObject.Password;
        }

        private void AssociatedObjectOnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            AssociatedObject.Password = Password;

            AssociatedObject.PasswordChanged -= AssociatedObjectOnPasswordChanged;
            AssociatedObject.PasswordChanged += AssociatedObjectOnPasswordChanged;
        }

        #endregion Event Handlers
    }
}
